
"""encoding='utf-8'
http://www.netbian.com/bizhidaquan/
抓取网站前三页图片并保存
"""
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests

from bs4 import BeautifulSoup


class All_get(object):
    def __init__(self):
        self.url = 'http://www.netbian.com/index.htm'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
        }

    # 获取源代码
    def My_get_ydm(self):
        response = requests.get(self.url, headers=self.headers)
        if response.status_code == 200:
            # print("response===")
            response.encoding="utf-8"
            # print(response.text)
            return response.text
        else:
            return None

    # 获取翻页的源代码
    def My_get_ydm2(self, url):
        response = requests.get(url, headers=self.headers)
        if response.status_code == 200:
            return response.text
        else:
            return None

    # 解析函数
    def My_js_index(self, response):
        global name2
        soup = BeautifulSoup(response, 'lxml')
        # soup.encoding="utf-8"
        # print('soup=>',soup)
        data = soup.find_all('div', class_="list")
        # data.encoding='utf-8'
        # print('data=>',data)
        lsit1 = []
        for i in data:
            nam = str(i)
            soup2 = BeautifulSoup(nam, 'lxml')
            data_list = soup2.find_all('img')
            for data in data_list:
                lsit1.append(data)
        for i in lsit1:
            name = i['src']
            print(name)
            respo = requests.get(name)#url转图片
            resp = respo.content#url转图片流
            nam = str(name)#应该是编码问题
            nam1 = nam.rindex('.')
            print(nam1)
            nam2 = nam[nam1:]
            print(nam2)
            with open(f'/Users/apple/pythonDownload/images/P/图片{name2}' + nam2, 'ab') as f:
                f.write(resp)
            name2 += 1
        print('图片保存成功')

    # 操控函数
    def My_main(self):
        self.My_js_index(self.My_get_ydm())

    # 翻页函数
    def My_main2(self):
        global a
        if a == 1:
            url = 'http://www.netbian.com/index_{}.htm'
            for i in range(2, 4):
                url1 = url.format(i)
                self.My_js_index(self.My_get_ydm2(url1))
            a += 1
        a += 1


if __name__ == '__main__':
    name2 = 1
    All = All_get()
    a = 0
    All.My_main()
    # a += 1
    # All.My_main2()
